//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Controls
{
	public class Title : Control
	{
		CSContext csContext = CSContext.Current;

		public TitleFormat TitleFormat
		{
			get
			{
				object o = ViewState["TitleFormat"];
				if (o == null)
					return TitleFormat.View;
				else
					return (TitleFormat) o;
			}
			set
			{
				ViewState["TitleFormat"] = value;
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			string title = FileGalleryResourceManager.GetString("Files_Title");
			string sep = FileGalleryResourceManager.GetString("Files_Title_Seperator");
			string name;

			switch (TitleFormat)
			{
				case TitleFormat.View:
					name = GetObjectName();					
					if (name != "")
						title += sep + name;

					break;

				case TitleFormat.EditUpload:

					if (csContext.PostID > 0)
						title += sep + string.Format(FileGalleryResourceManager.GetString("Files_Title_EditFile"), GetObjectName());
					else
						title += sep + FileGalleryResourceManager.GetString("Files_Title_UploadFile");

					break;

				case TitleFormat.Delete:

					title += sep + string.Format(FileGalleryResourceManager.GetString("Files_Title_DeleteFile"), GetObjectName());

					break;

				case TitleFormat.Download:

					title += sep + string.Format(FileGalleryResourceManager.GetString("Files_Title_DownloadFile"), GetObjectName());

					break;

				case TitleFormat.Upload:

					title = FileGalleryResourceManager.GetString("Files_Title_UploadFile");

					break;

				case TitleFormat.Tags:

					name = GetObjectName();					
					if (name != "")
						title += sep + name;

					if (csContext.Tags != null && csContext.Tags.Length > 0)
						title += sep + string.Format(FileGalleryResourceManager.GetString("Files_Title_Tags"), string.Join(", ", csContext.Tags));
					else
						title += sep + FileGalleryResourceManager.GetString("Files_Title_TagsHome");


					break;
			}

            if(csContext.ApplicationKey != null && Folders.GetFolder(csContext.ApplicationKey,true).IsPrivate)
            {
                UsersOnline.PrivateLocation();
            }
            else
            {
                if(TitleFormat == TitleFormat.Upload || TitleFormat == TitleFormat.EditUpload)
                {
                       UsersOnline.SetLocation(title,null);
                }
                else
                {
                    UsersOnline.SetLocation(title);
                }
            }

			Head.AddTitle(title, this.Context);
		}

		protected string GetObjectName()
		{
			if (csContext.PostID > 0)
			{
				Entry e = Entries.GetEntry(csContext.PostID);
				if (e != null)
					return e.Subject;
				else
					return "";
			}
			else if (csContext.CategoryID > 0)
			{
				int sectionID = -1;
				if (csContext.SectionID > 0)
					sectionID = csContext.SectionID;
				else if (!Globals.IsNullorEmpty(csContext.ApplicationKey))
				{
					sectionID = Folders.GetFolder(csContext.ApplicationKey).SectionID;
				}

				PostCategory pc = PostCategories.GetCategory(csContext.CategoryID, sectionID);
				if (pc != null)
					return pc.Name;
				else
					return "";
			}
			else if (!Globals.IsNullorEmpty(csContext.ApplicationKey))
			{
				Folder f = Folders.GetFolder(csContext.ApplicationKey);
				if (f != null)
					return f.Name;
				else
					return "";
			}
			else if (csContext.SectionID > 0)
			{
				Folder f = Folders.GetFolder(csContext.ApplicationKey);
				if (f != null)
					return f.Name;
				else
					return "";
			}
			else if (csContext.GroupID > 0)
			{
				Group g = Folders.GetFolderGroup(csContext.GroupID, true, false);
				if (g != null)
					return g.Name;
				else
					return "";
			}
			else
				return "";
		}
	}

	public enum TitleFormat
	{
		View,
		EditUpload,
		Delete,
		Download,
		Upload,
		Tags
	}
}
